﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.Statistics;

using System.Data;
using System.Data.Objects;
using System.Data.Common;
using System.Data.Entity;
using MINERP.DAL;
//using MINERP.MODEL.Statistics;
using MINERP.DAL.Statistics;
using MINERP.COM;
using MINERP.BLL.CustomerManage;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.Statistics
{
    public class FinanceBLL
    {
        /// <summary>
        /// 获得订单产品列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<OrderProductEntity> GetOrderProductList(int id)
        {
            List<OrderProductEntity> list = new List<OrderProductEntity>();

            Shop shop = StatisticsDAL.GetShopObj(id);
            if (!shop.Tb_OrderBag.IsLoaded)
            {
                shop.Tb_OrderBag.Load();
            }
            foreach (var item in shop.Tb_OrderBag)
            {

                if (item.Status == (int)OrderBagStatus.Doing)
                {
                    if (!item.orderInfo.IsLoaded)
                    {
                        item.orderInfo.Load();
                    }
                    foreach (var orderProduct in item.orderInfo)
                    {
                        if (orderProduct.status == (int)OrderInfoStatus.NoCheckCount)
                        {
                            OrderProductEntity orderProductEntity = new OrderProductEntity();
                            if (!orderProduct.Tb_OrderBagReference.IsLoaded)
                            {
                                orderProduct.Tb_OrderBagReference.Load();
                            }
                            orderProductEntity.StartOrderTime = orderProduct.Tb_OrderBag.OrderTime;
                            orderProductEntity.CtmName = orderProduct.CtmName;
                            orderProductEntity.OrderProductId = orderProduct.OrderProductId;
                            if (!orderProduct.Tb_ProductReference.IsLoaded)
                            {
                                orderProduct.Tb_ProductReference.Load();
                            }
                            orderProductEntity.OrderProductName = orderProduct.Tb_Product.ProductName;
                            orderProductEntity.PageNum = orderProduct.PageNum;
                            orderProductEntity.PrePriceCount = orderProduct.PrePriceCount == null ? 0 : (decimal)orderProduct.PrePriceCount;
                            orderProductEntity.productNum = orderProduct.productNum;
                            orderProductEntity.statusName = "余额不足";
                            list.Add(orderProductEntity);
                        }
                    }
                }

            }

            return list;
        }
        /// <summary>
        /// 获得商户账单列表
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public List<BillEntity> GetBillList(MINERP.MODEL.FoundationModel.Shop_Info shopInfo)
        {
            List<BillEntity> list = new List<BillEntity>();
              int uncheckStatus=  (int)ShopBillStatus.UnCheckOut;
              using (newSampleYPDBEntities db = new newSampleYPDBEntities())
              {
                  Shop shop = db.Shop.FirstOrDefault(s=>s.ShopId ==shopInfo.ShopId);
                  if (!shop.SHOP_Bill.IsLoaded)
                  {
                      shop.SHOP_Bill.Load();
                  }
                  else
                  {
                      shop.SHOP_Bill.Load(MergeOption.OverwriteChanges);
                  }


                  foreach (var item in shop.SHOP_Bill.Where(b => b.Status == uncheckStatus).ToList())
                  {
                      //if (item.Status == (int)ShopBillStatus.UnCheckOut)
                      //{
                      BillEntity billEntity = new BillEntity();
                      billEntity.IsChecked = false;
                      billEntity.BillID = item.BillId;
                      billEntity.BillOrderCode = item.BillOrderCode;
                      billEntity.CreateTime = item.CreateTime;
                      billEntity.BillCount = item.TotalPrice == null ? 0 : (double)item.TotalPrice;

                      if (!item.Bill_Detail.IsLoaded)
                      {
                          item.Bill_Detail.Load();
                      }
                      else
                      {
                          item.Bill_Detail.Load(MergeOption.OverwriteChanges);
                      }
                      foreach (var bill_detail in item.Bill_Detail)
                      {
                          if (bill_detail.IsCheckOut)
                              continue;
                          if (!bill_detail.Bill_Detail_Detail.IsLoaded)
                          {
                              bill_detail.Bill_Detail_Detail.Load();
                          }
                          else
                          {
                              bill_detail.Bill_Detail_Detail.Load(MergeOption.OverwriteChanges);
                          }
                          foreach (var bill_detail_detail in bill_detail.Bill_Detail_Detail)
                          {
                              billEntity.NoPayBillCount += bill_detail_detail.Count == 0 ? 0 : bill_detail_detail.Count;

                          }
                      }
                      list.Add(billEntity);
                      //}
                  }
              }
            return list;
        }
        /// <summary>
        /// 获得商户账单列表
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public List<BillEntity> GetBillList(MINERP.MODEL.FoundationModel.Shop_Info inshop,newSampleYPDBEntities db)
        {
            Shop shop = db.Shop.FirstOrDefault(sh=>sh.ShopId==inshop.ShopId);
            List<BillEntity> list = new List<BillEntity>();
            int uncheckStatus = (int)ShopBillStatus.UnCheckOut;
            if (!shop.SHOP_Bill.IsLoaded)
            {
                shop.SHOP_Bill.Load();
            }
            else
            {
                shop.SHOP_Bill.Load();
            }


            foreach (var item in shop.SHOP_Bill.Where(b => b.Status == uncheckStatus).ToList())
            {
                //if (item.Status == (int)ShopBillStatus.UnCheckOut)
                //{
                BillEntity billEntity = new BillEntity();
                billEntity.IsChecked = false;
                billEntity.BillID = item.BillId;
                billEntity.BillOrderCode = item.BillOrderCode;
                billEntity.CreateTime = item.CreateTime;
                billEntity.BillCount = item.TotalPrice == null ? 0 : (double)item.TotalPrice;

                if (!item.Bill_Detail.IsLoaded)
                {
                    item.Bill_Detail.Load();
                }
                else
                {
                    item.Bill_Detail.Load(MergeOption.OverwriteChanges);
                }
                foreach (var bill_detail in item.Bill_Detail)
                {
                    if (bill_detail.IsCheckOut)
                        continue;
                    if (!bill_detail.Bill_Detail_Detail.IsLoaded)
                    {
                        bill_detail.Bill_Detail_Detail.Load();
                    }
                    else
                    {
                        bill_detail.Bill_Detail_Detail.Load();
                    }
                    foreach (var bill_detail_detail in bill_detail.Bill_Detail_Detail)
                    {
                        billEntity.NoPayBillCount += bill_detail_detail.Count == 0 ? 0 : bill_detail_detail.Count;

                    }
                }
                list.Add(billEntity);
                //}
            }
            return list;
        }
        /// <summary>
        /// 获得业务员所属商户列表
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public List<ShopCrushInfo> GetSalesmanShopList(Salesman_Info salesman)
        {
            List<ShopCrushInfo> shopcrushInfoList = new List<ShopCrushInfo>();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<Shop> shoplist = enty.Shop.Where(s => s.Salesman == salesman.SalesmanName&&s.Balance<0).ToList();
                foreach (var shop in shoplist)
                {
                    ShopCrushInfo shopcrushInfo = new ShopCrushInfo();
                    shopcrushInfo.Balance = (decimal)shop.Balance;
                    shopcrushInfo.ShopId = shop.ShopId;
                    shopcrushInfo.ShopName = shop.shopName;
                    shopcrushInfoList.Add(shopcrushInfo);
                }
            }
            return shopcrushInfoList;
        }

        public SHOP_Bill GetBillObj(int id)
        {
            return StatisticsDAL.GetBillObj(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MINERP.MODEL.FoundationModel.Shop_Info GetShopObj(int id)
        {
            MINERP.MODEL.FoundationModel.Shop_Info shopInfo= null;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop shop = db.Shop.FirstOrDefault(s => s.ShopId == id);
                    shopInfo = new MINERP.MODEL.FoundationModel.Shop_Info()
                    {
                        Address = shop.address,
                        Area = shop.Area,
                        AreaPrice = shop.AreaPrice==null?0:(decimal)shop.AreaPrice,
                        Balance = shop.Balance==null?0:(decimal)shop.Balance,
                        CheckOutType = shop.CheckOutType,
                        CreditLevel = shop.CreditLevel==null?0:(int)shop.CreditLevel,
                        CreditLimit = shop.CreditLimit==null?0:(decimal)shop.CreditLimit,
                        EmailAddr = shop.EmailAddr,
                        FtpLoginName = shop.FtpLoginName,
                        FtpLoginPWD = shop.FtpLoginPWD,
                        GraceDay = shop.GraceDay==null?0:(int)shop.GraceDay,
                        IsAssignByCustom = shop.IsAssignByCustom,
                        IsNeedPrintUPrice = shop.IsNeedPrintUPrice==null?true:(bool)shop.IsNeedPrintUPrice,
                        MnemonicCode = shop.mnemonicCode,
                        MSN = shop.MSN,
                        OrderBalance = shop.OrderBalance==null?0:(decimal)shop.OrderBalance,
                        OverDay = shop.OverDay==null?0:(int)shop.OverDay,
                        PaperFaceId = shop.PaperFaceId==null?0:(int)shop.PaperFaceId,
                        PaperTypeId = shop.PaperTypeId==null?0:(int)shop.PaperTypeId,
                        Priority = shop.Priority==null?0:(int)shop.Priority,
                        QQ = shop.QQ==null?"":shop.QQ,
                        Remark = shop.Remark,
                        Salesman = shop.Salesman,
                        ShopId = shop.ShopId,
                        ShopKeeper = shop.shopKeeper,
                        ShopName = shop.shopName,
                        Status = shop.Status==null?0:(int)shop.Status,
                        Tel1 = shop.Tel1,
                        Tel2 = shop.Tel2,
                        XmlVersion = shop.XmlVersion==null?0:(int)shop.XmlVersion

                    };
                }
            }
            catch (Exception ex)
            {
                throw ex; 
            }
            return shopInfo;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public MINERP.MODEL.FoundationModel.Shop_Info GetShopObj(string name)
        {
            MINERP.MODEL.FoundationModel.Shop_Info shopInfo = null;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop shop = db.Shop.FirstOrDefault(s => s.shopName == name);
                    shopInfo = new MINERP.MODEL.FoundationModel.Shop_Info()
                    {
                        Address = shop.address,
                        Area = shop.Area,
                        AreaPrice = shop.AreaPrice == null ? 0 : (decimal)shop.AreaPrice,
                        Balance = shop.Balance == null ? 0 : (decimal)shop.Balance,
                        CheckOutType = shop.CheckOutType,
                        CreditLevel = shop.CreditLevel == null ? 0 : (int)shop.CreditLevel,
                        CreditLimit = shop.CreditLimit == null ? 0 : (decimal)shop.CreditLimit,
                        EmailAddr = shop.EmailAddr,
                        FtpLoginName = shop.FtpLoginName,
                        FtpLoginPWD = shop.FtpLoginPWD,
                        GraceDay = shop.GraceDay == null ? 0 : (int)shop.GraceDay,
                        IsAssignByCustom = shop.IsAssignByCustom,
                        IsNeedPrintUPrice = shop.IsNeedPrintUPrice == null ? true : (bool)shop.IsNeedPrintUPrice,
                        MnemonicCode = shop.mnemonicCode,
                        MSN = shop.MSN,
                        OrderBalance = shop.OrderBalance == null ? 0 : (decimal)shop.OrderBalance,
                        OverDay = shop.OverDay == null ? 0 : (int)shop.OverDay,
                        PaperFaceId = shop.PaperFaceId == null ? 0 : (int)shop.PaperFaceId,
                        PaperTypeId = shop.PaperTypeId == null ? 0 : (int)shop.PaperTypeId,
                        Priority = shop.Priority == null ? 0 : (int)shop.Priority,
                        QQ = shop.QQ == null ? "" : shop.QQ,
                        Remark = shop.Remark,
                        Salesman = shop.Salesman,
                        ShopId = shop.ShopId,
                        ShopKeeper = shop.shopKeeper,
                        ShopName = shop.shopName,
                        Status = shop.Status == null ? 0 : (int)shop.Status,
                        Tel1 = shop.Tel1,
                        Tel2 = shop.Tel2,
                        XmlVersion = shop.XmlVersion == null ? 0 : (int)shop.XmlVersion

                    };
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return shopInfo;
        }
        /// <summary>
        /// 获取所有商户列表
        /// </summary>
        /// <returns></returns>
        public List<ShopEntity> GetShopData()
        {
            List<ShopEntity> list = new List<ShopEntity>();
            //foreach (var item in StatisticsDAL.GetShopObjList())
            //{
            //    ShopEntity shopEntity = new ShopEntity();
            //    shopEntity.address = item.address;
            //    shopEntity.Balance = Math.Round(item.Balance == null ? 0 : (decimal)item.Balance, 2);
            //    shopEntity.OrderBalance = Math.Round(item.OrderBalance == null ? 0 : (decimal)item.OrderBalance, 2);
            //    shopEntity.CheckOutType = item.CheckOutType == null ? 4 : (int)item.CheckOutType;
            //    shopEntity.CreditLimit = Math.Round(item.CreditLimit == null ? 0 : (decimal)item.CreditLimit, 2);
            //    shopEntity.OverDay = item.OverDay == null ? 0 : (int)item.OverDay;
            //    shopEntity.GraceDay = item.GraceDay == null ? 0 : (int)item.GraceDay;
            //    shopEntity.mnemonicCode = item.mnemonicCode;
            //    shopEntity.ShopId = item.ShopId;
            //    shopEntity.shopKeeper = item.shopKeeper;
            //    shopEntity.shopName = item.shopName;
            //    shopEntity.Tel1 = item.Tel1;
            //    shopEntity.Tel2 = item.Tel2;
            //    shopEntity.Area = item.Area;
            //    shopEntity.AreaPrice = item.AreaPrice == null ? 0 : (decimal)item.AreaPrice;
            //    shopEntity.QQ = item.QQ;
            //    shopEntity.MSN = item.MSN;
            //    shopEntity.Email = item.EmailAddr;
            //    shopEntity.SalesMan = item.Salesman;
            //    shopEntity.AccountControlTechId = item.AccountControlTechId;
            //    shopEntity.AlarmDays = item.AlarmDays;
            //    shopEntity.AlarmLimit = item.AlarmLimit;
            //    shopEntity.Remark = item.Remark;

            //    switch (item.CheckOutType)
            //    {
            //        case 0:
            //            shopEntity.CheckOutTypeName = "预付款";
            //            break;
            //        case 1:
            //            shopEntity.CheckOutTypeName = "信用额度";
            //            break;
            //        case 2:
            //            shopEntity.CheckOutTypeName = "现结";
            //            break;
            //        case 3:
            //            shopEntity.CheckOutTypeName = "结账日期";
            //            break;
            //        case 4:
            //            shopEntity.CheckOutTypeName = "结账日期与信用额度";
            //            break;
            //        case 5:
            //            shopEntity.CheckOutTypeName = "先结";
            //            break;
            //        case 6:
            //            shopEntity.CheckOutTypeName = "代收";
            //            break;
            //        default:
            //            break;

            //    }
            //    list.Add(shopEntity);
            //}
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                
                List<Shop> listShop = db.Shop.Where(p=>p.Status==1).ToList();
                foreach (var item in listShop)
                {
                    ShopEntity shopEntity = new ShopEntity();
                    shopEntity.address = item.address;
                    shopEntity.Balance = Math.Round(item.Balance == null ? 0 : (decimal)item.Balance, 2);
                    shopEntity.OrderBalance = Math.Round(item.OrderBalance == null ? 0 : (decimal)item.OrderBalance, 2);
                    shopEntity.CheckOutType = item.CheckOutType == null ? 4 : (int)item.CheckOutType;
                    shopEntity.CreditLimit = Math.Round(item.CreditLimit == null ? 0 : (decimal)item.CreditLimit, 2);
                    shopEntity.OverDay = item.OverDay == null ? 0 : (int)item.OverDay;
                    shopEntity.GraceDay = item.GraceDay == null ? 0 : (int)item.GraceDay;
                    shopEntity.mnemonicCode = item.mnemonicCode;
                    shopEntity.ShopId = item.ShopId;
                    shopEntity.shopKeeper = item.shopKeeper;
                    shopEntity.shopName = item.shopName;
                    shopEntity.Tel1 = item.Tel1;
                    shopEntity.Tel2 = item.Tel2;
                    shopEntity.Area = item.Area;
                    shopEntity.AreaPrice = item.AreaPrice == null ? 0 : (decimal)item.AreaPrice;
                    shopEntity.QQ = item.QQ;
                    shopEntity.MSN = item.MSN;
                    shopEntity.Email = item.EmailAddr;
                    shopEntity.SalesMan = item.Salesman;
                    shopEntity.AccountControlTechId = item.AccountControlTechId;
                    shopEntity.AlarmDays = item.AlarmDays;
                    shopEntity.AlarmLimit = item.AlarmLimit;
                    shopEntity.Remark = item.Remark;
                    item.Shop_Paper_Price.Load();
                    Shop_Paper_Price spp = item.Shop_Paper_Price.FirstOrDefault(p => p.PaperTypeId == item.PaperTypeId);
                    if (spp != null)
                    {
                        shopEntity.AreaPrice = spp.UnitPrice;
                    }
                    
                    switch (item.CheckOutType)
                    {
                        case 0:
                            shopEntity.CheckOutTypeName = "预付款";
                            break;
                        case 1:
                            shopEntity.CheckOutTypeName = "信用额度";
                            break;
                        case 2:
                            shopEntity.CheckOutTypeName = "现结";
                            break;
                        case 3:
                            shopEntity.CheckOutTypeName = "结账日期";
                            break;
                        case 4:
                            shopEntity.CheckOutTypeName = "结账日期与信用额度";
                            break;
                        case 5:
                            shopEntity.CheckOutTypeName = "先结";
                            break;
                        case 6:
                            shopEntity.CheckOutTypeName = "代收";
                            break;
                        default:
                            break;

                    }
                    list.Add(shopEntity);
                }
            }
            return list;
        }

        /// <summary>
        /// 通过助记码获取商户列表
        /// </summary>
        /// <returns></returns>
        public List<ShopEntity> GetShopDataByMnemoniccode(string mnemonicCode)
        {
            List<ShopEntity> list = new List<ShopEntity>();
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                List<Shop> shopList = entity.Shop.Where(s => s.mnemonicCode.Contains(mnemonicCode)).ToList();
                foreach (var item in shopList)
                {
                    ShopEntity shopEntity = new ShopEntity();
                    shopEntity.address = item.address;
                    shopEntity.Balance = Math.Round(item.Balance == null ? 0 : (decimal)item.Balance, 2);
                    shopEntity.OrderBalance = Math.Round(item.OrderBalance == null ? 0 : (decimal)item.OrderBalance, 2);
                    shopEntity.CheckOutType = item.CheckOutType == null ? 4 : (int)item.CheckOutType;
                    shopEntity.CreditLimit = Math.Round(item.CreditLimit == null ? 0 : (decimal)item.CreditLimit, 2);
                    shopEntity.OverDay = item.OverDay == null ? 0 : (int)item.OverDay;
                    shopEntity.GraceDay = item.GraceDay == null ? 0 : (int)item.GraceDay;
                    shopEntity.mnemonicCode = item.mnemonicCode;
                    shopEntity.ShopId = item.ShopId;
                    shopEntity.shopKeeper = item.shopKeeper;
                    shopEntity.shopName = item.shopName;
                    shopEntity.Tel1 = item.Tel1;
                    shopEntity.Tel2 = item.Tel2;
                    shopEntity.Area = item.Area;
                    shopEntity.AreaPrice = item.AreaPrice == null ? 0 : (decimal)item.AreaPrice;
                    shopEntity.QQ = item.QQ;
                    shopEntity.MSN = item.MSN;
                    shopEntity.Email = item.EmailAddr;
                    shopEntity.SalesMan = item.Salesman;
                    shopEntity.AccountControlTechId = item.AccountControlTechId;
                    shopEntity.AlarmDays = item.AlarmDays;
                    shopEntity.AlarmLimit = item.AlarmLimit;

                    switch (item.CheckOutType)
                    {
                        case 0:
                            shopEntity.CheckOutTypeName = "预付款";
                            break;
                        case 1:
                            shopEntity.CheckOutTypeName = "信用额度";
                            break;
                        case 2:
                            shopEntity.CheckOutTypeName = "现结";
                            break;
                        case 3:
                            shopEntity.CheckOutTypeName = "结账日期";
                            break;
                        case 4:
                            shopEntity.CheckOutTypeName = "结账日期与信用额度";
                            break;
                        case 5:
                            shopEntity.CheckOutTypeName = "先结";
                            break;
                        default:
                            break;

                    }
                    list.Add(shopEntity);
                }
            }
            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Shop> GetShops()
        {
            return StatisticsDAL.GetShopObjList();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public orderInfo GetOrderInfoObj(int id)
        {
            return StatisticsDAL.GetOrderInfo(id);
        }

        public List<tb_Tech> GetTechList()
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.tb_Tech.ToList();
            }
        }

        public tb_Tech GetTech(int id)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.tb_Tech.FirstOrDefault(t => t.TechId == id);
            }
        }


        public tb_Tech GetTech(string name)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.tb_Tech.FirstOrDefault(t => t.TechName == name);
            }
        }
        /// <summary> 
        /// 
        /// </summary>
        public void Update()
        {
            StatisticsDAL.Update();
        }

        /// <summary>
        /// 激活该商户下能够被激活的产品
        /// </summary>
        /// <param name="_shop"></param>
        public void ActiveOrder(MINERP.MODEL.FoundationModel.Shop_Info shopInfo)
        {
            using (newSampleYPDBEntities db=new newSampleYPDBEntities ())
            {
                Shop curShop = db.Shop.FirstOrDefault(s => s.ShopId == shopInfo.ShopId);
                if (curShop!=null)
                {
                    if (!curShop.Tb_OrderBag.IsLoaded)
                    {
                        curShop.Tb_OrderBag.Load();                       
                    }

                    foreach (var OG in curShop.Tb_OrderBag.ToList())
                    {
                        if (!OG.orderInfo .IsLoaded)
                        {
                            OG.orderInfo.Load();
                        }
                        foreach (var oi in OG.orderInfo.ToList().Where(o=>o.status==(int)OrderInfoStatus.WaitPay||o.status==(int)OrderInfoStatus.WaitPayAndWaitMovePhoto))
                        {
                            BillBLL.CheckOrderCount(oi,curShop,false,db);
                        }
                    }
                }

                db.SaveChanges();
            }
        }

        /// <summary>
        /// 激活该业务员下能够被激活的产品
        /// </summary>
        /// <param name="_shop"></param>
        public void ActiveSalesmanOrder(Salesman_Info salesman)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shoplist = db.Shop.Where(s => s.Salesman ==salesman.SalesmanName).ToList();

                foreach (var curShop in shoplist)
	            {
	
                    if (!curShop.Tb_OrderBag.IsLoaded)
                    {
                        curShop.Tb_OrderBag.Load();
                    }

                    foreach (var OG in curShop.Tb_OrderBag.ToList())
                    {
                        if (!OG.orderInfo.IsLoaded)
                        {
                            OG.orderInfo.Load();
                        }
                        foreach (var oi in OG.orderInfo.ToList().Where(o => o.status == (int)OrderInfoStatus.WaitPay || o.status == (int)OrderInfoStatus.WaitPayAndWaitMovePhoto))
                        {
                            BillBLL.CheckOrderCount(oi, curShop, false, db);
                        }
                    }
                }

                db.SaveChanges();
            }
        }

        /// <summary>
        /// 获得业务员信息列表
        /// </summary>
        /// <returns></returns>
        public List<Salesman_Info> GetSalesmanInfoList()
        {
            List<Salesman_Info> salesInfoList = new List<Salesman_Info>();

            using(newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<SalesmanBalance> sales = enty.SalesmanBalance.ToList();
                foreach (var item in sales)
                {
                    Salesman_Info salesmanInfo = new Salesman_Info()
                    {
                        Balance = item.Balance,
                        Remark = item.Remark,
                        SalesmanId = item.SalesmanId,
                        SalesmanName = item.SalesmanName,
                        Tel1 = item.Tel1
                    };
                    salesInfoList.Add(salesmanInfo);
                }
            }
            return salesInfoList;
        }

        /// <summary>
        /// 获得业务员信息
        /// </summary>
        /// <returns></returns>
        public Salesman_Info GetSalesmanInfo(string salesmanName)
        {
            Salesman_Info salesmanInfo=null;
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                SalesmanBalance saleman = enty.SalesmanBalance.FirstOrDefault(s=>s.SalesmanName ==salesmanName);

                salesmanInfo = new Salesman_Info()
                {
                    Balance = saleman.Balance,
                    Remark = saleman.Remark,
                    SalesmanId = saleman.SalesmanId,
                    SalesmanName = saleman.SalesmanName,
                    Tel1 = saleman.Tel1
                };   
            }
            return salesmanInfo;
        }

        /// <summary>
        /// 获得业务员账单列表
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public List<BillEntity> GetSalesmanBillList(Salesman_Info salesman)
        {
            List<BillEntity> list = new List<BillEntity>();
            int uncheckStatus = (int)ShopBillStatus.UnCheckOut;
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<Shop> shoplist = enty.Shop.Where(s => s.Salesman == salesman.SalesmanName).ToList();
                foreach (var shop in shoplist)
                {
                    if (!shop.SHOP_Bill.IsLoaded)
                    {
                        shop.SHOP_Bill.Load();
                    }
                    else
                    {
                        shop.SHOP_Bill.Load(MergeOption.OverwriteChanges);
                    }


                    foreach (var item in shop.SHOP_Bill.Where(b => b.Status == uncheckStatus).ToList())
                    {
                        //if (item.Status == (int)ShopBillStatus.UnCheckOut)
                        //{
                        BillEntity billEntity = new BillEntity();
                        billEntity.IsChecked = false;
                        billEntity.BillID = item.BillId;
                        billEntity.BillOrderCode = item.BillOrderCode;
                        billEntity.CreateTime = item.CreateTime;
                        billEntity.BillCount = item.TotalPrice == null ? 0 : (double)item.TotalPrice;

                        if (!item.Bill_Detail.IsLoaded)
                        {
                            item.Bill_Detail.Load();
                        }
                        else
                        {
                            item.Bill_Detail.Load(MergeOption.OverwriteChanges);
                        }
                        foreach (var bill_detail in item.Bill_Detail)
                        {
                            if (bill_detail.IsCheckOut)
                                continue;
                            if (!bill_detail.Bill_Detail_Detail.IsLoaded)
                            {
                                bill_detail.Bill_Detail_Detail.Load();
                            }
                            else
                            {
                                bill_detail.Bill_Detail_Detail.Load(MergeOption.OverwriteChanges);
                            }
                            foreach (var bill_detail_detail in bill_detail.Bill_Detail_Detail)
                            {
                                billEntity.NoPayBillCount += bill_detail_detail.Count == 0 ? 0 : bill_detail_detail.Count;

                            }
                        }
                        list.Add(billEntity);

                    }
                }
            }
            return list;
        }


         /// <summary>
        /// 获取某商户未生产完成的产品的总价值
        /// </summary>
        /// <param name="inStartData"></param>
        /// <param name="inEndDate"></param>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public void GetUnCompleteProductBillByShop( int inShopId)
        {
            int Bill_Detail_Id = 1;     //用于报表中的关联ID
            double Bill_Price = 0;
            SHOP_Bill_Info sbInfo = null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                Shop curShop = db.Shop.FirstOrDefault(s => s.ShopId == inShopId);
                //sbInfo = new SHOP_Bill_Info();
                //sbInfo.List_Detail = new List<Bill_Detail_Info>();
                List<orderInfo> listOrder = db.orderInfo.Where(oi => oi.Tb_OrderBag.Shop.ShopId == inShopId && oi.status == 1).OrderBy(o => o.OrderProductId).ToList();
                //ShowMessage(listOrder.Count, 0);
                int iCount = 0;//计数器
                foreach (var oi in listOrder)
                {
                    iCount++;
                    //ShowMessage(iCount, 1);
                    decimal orderInfoPrice = 0;//此产品的真实价格
                    #region 加载数据库
                    if (!oi.Tb_ProductReference.IsLoaded)
                    {
                        oi.Tb_ProductReference.Load();
                    }
                    if (!oi.Tb_Product.tb_CostTypeReference.IsLoaded)
                    {
                        oi.Tb_Product.tb_CostTypeReference.Load();
                    }
                    if (!oi.Tb_Product.Tb_PorductCategoryReference.IsLoaded)
                    {
                        oi.Tb_Product.Tb_PorductCategoryReference.Load();
                    }
                    if (!curShop.Shop_Product_Price.IsLoaded)
                    {
                        curShop.Shop_Product_Price.Load();
                    }
                    if (!oi.photoInfo.IsLoaded)
                    {
                        oi.photoInfo.Load();
                    }
                    if (!oi.Tb_OrderBagReference.IsLoaded)
                    {
                        oi.Tb_OrderBagReference.Load();
                    }
                    string orderBagName = oi.Tb_OrderBag.BagName.Split('_')[0];

                    #endregion
                    Bill_Detail_Info dbinfo = new Bill_Detail_Info() { BillDetailId = Bill_Detail_Id, Remark = oi.Remark, OrderInfoCode = oi.OrderProductId.ToString(), OrderProductName = oi.OrderProductName, CtmName = oi.CtmName, OrderBagName = orderBagName };
                    Bill_Detail_Id++;
                    dbinfo.ListDetail_Detail = new List<Bill_Detail_Detail_Info>();
                    orderInfoPrice = MINERP.BLL.CustomerManage.BillBLL.GetOrderPrice(curShop, oi, db, dbinfo);//获取产品的价值


                    dbinfo.BillDetailId = Bill_Detail_Id;

                    //sbInfo.List_Detail.Add(dbinfo);
                    //Bill_Detail_Id++;
                    Bill_Price += (double)orderInfoPrice;
                }
                curShop.OrderBalance = (decimal)Bill_Price;
                db.SaveChanges();
            }
        }

    }
}
